﻿#include "stdafx.h"
#include "CodeConvert.h"
#include <locale.h>

_NAMESPACE_BEGIN

using namespace std;

void CodeConvert::MultiByte2Unicode( const char* in,tstring& out,int inlen )
{
	Char2WChar(CP_ACP,in,out,inlen);
}
void CodeConvert::MultiByte2Unicode( const char* in,TCHAR out[],int outsize,int inlen )
{
	Char2WChar(CP_ACP,in,out,outsize,inlen);
}

void CodeConvert::Unicode2MultiByte( const TCHAR* in,string& out,int inlen )
{
	WChar2Char(CP_ACP,in,out,inlen);
}

void CodeConvert::Unicode2MultiByte( const TCHAR* in,char out[],int outsize,int inlen/*=-1*/ )
{
	WChar2Char(CP_ACP,in,out,outsize,inlen);
}

void CodeConvert::Unicode2UTF8( const TCHAR* in,string& out,int inlen/*=-1*/ )
{
	WChar2Char(CP_UTF8,in,out,inlen);
}

void CodeConvert::Unicode2UTF8( const TCHAR* in,char out[],int outsize,int inlen/*=-1*/ )
{
	WChar2Char(CP_UTF8,in,out,outsize,inlen);
}
void CodeConvert::UTF8_2Unicode( const char* in,tstring& out,int inlen/*=-1*/ )
{
	Char2WChar(CP_UTF8,in,out,inlen);
}

void CodeConvert::UTF8_2Unicode( const char* in,TCHAR out[],int outsize,int inlen/*=-1*/ )
{
	Char2WChar(CP_UTF8,in,out,outsize,inlen);
}
void CodeConvert::WChar2Char( UINT codepage,const TCHAR* in,string& out,int inlen/*=-1*/ )
{
	int size=WideCharToMultiByte(codepage, 0,in,inlen,NULL,0,NULL, 0);
	if(inlen>0)//WideCharToMultiByte的bug，如果chWideChar不为－1，则得到的size不包括\0
		size++;
	char* words;
	newarray(words,char,size);
	WideCharToMultiByte(codepage, 0,in,inlen,words,size,NULL, 0);	
	out.assign(words,size-1);
	delarray(words);
}

void CodeConvert::WChar2Char( UINT codepage,const TCHAR* in,char out[],int outsize,int inlen/*=-1*/ )
{
	WideCharToMultiByte(codepage, 0,in,inlen,out,outsize, 0, 0);
}
void CodeConvert::Char2WChar( UINT codepage,const char* in,tstring& out,int inlen/*=-1*/ )
{
	//这里要注意inlen是字节数，汉字算2个
	int size=MultiByteToWideChar(codepage, 0,in,inlen,NULL,0);
	if(inlen>0)//MultiByteToWideChar的bug，如果cbMultiByte不为－1，则得到的size不包括\0
		size++;
	TCHAR* twords;
	newarray(twords,TCHAR,size);
	MultiByteToWideChar(codepage, 0,in,inlen,twords,size);
	out.assign(twords,size-1);
	delarray(twords);
}

void CodeConvert::Char2WChar( UINT codepage,const char* in,TCHAR out[],int outsize,int inlen/*=-1*/ )
{
	MultiByteToWideChar(codepage, 0,in, inlen,out,outsize);
}
bool CodeConvert::ExistChinese(CONST_REF(tstring) tstr)
{	
	string str;
	Unicode2MultiByte(tstr.c_str(),str);
	return tstr.size()!=str.size();
}





#define  CODECONVERT_CHOOSE_ANY_TYPE(T) else if(typeid(T)==type){\
out=boost::lexical_cast<tstring>(boost::any_cast<T>(a));}

void CodeConvert::Any2Unicode( CONST_REF(boost::any) a,tstring& out )
{
	const type_info& type=a.type();
	if(typeid(string)==type)
	{
		MultiByte2Unicode(boost::any_cast<string>(&a)->c_str(),out);
	}
	else if(typeid(wstring)==type)
	{
		out=boost::any_cast<wstring>(a);
	}
	CODECONVERT_CHOOSE_ANY_TYPE(bool)		CODECONVERT_CHOOSE_ANY_TYPE(char)		CODECONVERT_CHOOSE_ANY_TYPE(UCHAR)
		CODECONVERT_CHOOSE_ANY_TYPE(short)		CODECONVERT_CHOOSE_ANY_TYPE(USHORT)
		CODECONVERT_CHOOSE_ANY_TYPE(INT32)		CODECONVERT_CHOOSE_ANY_TYPE(UINT32)		CODECONVERT_CHOOSE_ANY_TYPE(float)
		CODECONVERT_CHOOSE_ANY_TYPE(INT64)		CODECONVERT_CHOOSE_ANY_TYPE(UINT64)		CODECONVERT_CHOOSE_ANY_TYPE(double)

}


_NAMESPACE_END